from selenium import webdriver
import time


dr = webdriver.Chrome(r'/home/python/Desktop/chromedriver')

dr.get('http://bj.58.com')
time.sleep(5)
# 定位到租房
el_zf = dr.find_element_by_xpath('/html/body/div[3]/div[1]/div[1]/div/div[1]/div[1]/span[1]/a')
el_zf.click()
# 获取当前打开的窗口
# dr.window_handles
print(dr.window_handles)
# 切换到最后一个窗口
# dr.switch_to_window(dr.window_handles[-1])
dr.switch_to.window(dr.window_handles[-1])
print(dr.current_url)
# 获取ａ节点
# dr.get('http://bj.58.com/chuzu/?PGTID=0d100000-0000-1476-9046-1764be38b4a0&ClickID=1')
time.sleep(5)
ela_list = dr.find_elements_by_xpath('/html/body/div[4]/div[1]/div[5]/div[2]/ul/li/div[2]/h2/a')
time.sleep(10)
print(ela_list)
title_url = []
for a in ela_list:
    dict_a = dict()
    # 获取标题
    dict_a['title'] = a.text
    # 获取链接
    dict_a['url'] = a.get_attribute('href')
    print(dict_a)
    title_url.append(dict_a)